package br.com.fiap.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.fiap.dao.VooDAO;
import br.com.fiap.entity.Voo;

public class VooDAOImpl extends AbstractDAOImpl<Voo> implements VooDAO {

	private static final long serialVersionUID = -3562190807598753269L;
	
	public List<Voo> findByNumero(Integer numero) {
		
		Session session = this.getHibernateSession();
		
		StringBuilder sql = new StringBuilder();
		
		sql.append(" select v from " + Voo.class.getCanonicalName() + " v ");
		
		if (numero != null) {
			sql.append(" where v.numero = :numero ");
		}
		
		Query query = session.createQuery(sql.toString());
		
		if (numero != null) {
			query.setParameter("numero", numero);
		}

		List<Voo> voos = (List<Voo>) query.list();
		
		session.clear();
		session.close();
		
		return voos;
		
	}
	
	@Override
	public Class<Voo> getEntityClass() {
		return Voo.class;
	}
	
}
